package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hu
 * @date 2025/3/9
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> listAll(StudentQueryParam queryParam) {
        Integer page = queryParam.getPage();
        Integer pageSize = queryParam.getPageSize();
        PageHelper.startPage(page, pageSize);
        List<Student> students = studentMapper.listAll(queryParam);
        Page<Student> p = (Page<Student>) students;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void delById(List<Long> ids) {
        studentMapper.delById(ids);
    }

    @Override
    public void add(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        if (score > 100 || score < 0) {
            throw new NumberFormatException("违纪扣分在0-100之间，请重新输入");
        }
        studentMapper.updateScore(id, score);
    }


}
